package com.yufuchang;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by yufuchang on 2020/8/11.
 */
@ControllerAdvice
public class ControllerAdviceHandler {
    //返回固定文字
//    @ExceptionHandler(MaxUploadSizeExceededException.class)
//    public void uploadException(MaxUploadSizeExceededException exception, HttpServletResponse resp)
//            throws IOException {
//        resp.setContentType("text/html;charset=utf-8");
//        PrintWriter out = resp.getWriter();
//        out.write("上传文件大小超出20KB限制!");
//        out.flush();
//        out.close();
//    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView uploadException(MaxUploadSizeExceededException exception, HttpServletResponse resp)
            throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg","网页版超出20KB大小限制");
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
